// pages/member/CommentDetails/CommentDetails.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    content: "",
    order_id: "",
    goods_id: "",
    order_goods_id: "",
    //评价时间
    created_at: "",
    //追评
    append: null,
    //全部回复
    reply: null,
    head_img_url: "",
    images: [],
    nick_name: "",
    level_name: "",
    has_one_order_goods: null,
    has_many_reply: [],
    //主评论id
    comment_id: "",
    //1-代表回复主评论 2-回复某人
    reply_type: 1,
    reply_placeholder: "回复@昵称:",
    //当前被回复对象
    reply_one: "",
    uid: "",
    level:0,
    worker_score:null,
    worker_name:'安装师傅',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.order_id) {
      this.setData({
        order_id: options.order_id,
      });
    }
    if (options.goods_id) {
      this.setData({
        goods_id: options.goods_id,
      });
    }
    if (options.order_goods_id) {
      this.setData({
        order_goods_id: options.order_goods_id,
      });
    }
    if (options.comment_id) {
      this.setData({
        comment_id: options.comment_id,
      });
    }
    if (options.uid) {
      this.setData({
        uid: options.uid,
      });
    }

    this._getNetData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  previewImage(evt){
    let {images,src} = evt.currentTarget.dataset;
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: images // 需要预览的图片http链接列表
    });
  },
  onShareTimeline:function(){
    let json = {
      title: '评价详情',
      query: `/packageD/member/CommentDetails/CommentDetails?`,
    }
    json.query += `order_id=${this.data.order_id}&goods_id=${this.data.goods_id}&order_goods_id=${this.data.order_goods_id}&comment_id=${this.data.comment_id}`
    if(this.data.uid){
      json.query +=`&uid=${this.data.uid}`
    }
    if(this.data.istype){
      json.query += `&istype=${this.data.istype}`
    }
    return json
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let json = {
      title: '评价详情',
      path: `/packageD/member/CommentDetails/CommentDetails?`,
    }
    json.path += `order_id=${this.data.order_id}&goods_id=${this.data.goods_id}&order_goods_id=${this.data.order_goods_id}&comment_id=${this.data.comment_id}`
    if(this.data.uid){
      json.path +=`&uid=${this.data.uid}`
    }
    if(this.data.istype){
      json.path += `&istype=${this.data.istype}`
    }
    return json
  },
  _getNetData() {
    let that = this;
    let urlStr = app.getNetAddresss("goods.comment.get-order-goods-comment");
    urlStr += "&order_id=" + this.data.order_id;
    urlStr += "&goods_id=" + this.data.goods_id;
    urlStr += "&order_goods_id=" + this.data.order_goods_id;
    urlStr += "&comment_id=" + this.data.comment_id;
    app._getNetWork({
      url: urlStr,
      success:  (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let myData = res.data;
          that.setData({
            content: myData.content,
            created_at: myData.created_at,
            append: myData.append,
            reply: myData.reply_name,
            head_img_url: myData.head_img_url,
            images: myData.images,
            nick_name: myData.nick_name,
            level_name: myData.level_name,
            has_one_order_goods: myData.has_one_order_goods,
            has_many_reply: myData.has_many_reply,
            comment_id: myData.id,
            level:myData.level
          });
          if(myData.has_one_live_install_comment && myData.has_one_live_install_comment.worker_score){
            this.setData({
              worker_score:myData.has_one_live_install_comment.worker_score
            });
          }
          if(myData.another_name && myData.another_name.worker_name){
            this.setData({worker_name: myData.another_name.worker_name});
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //回复主评论
  submitReplyForComment() {
    this.setData({
      reply_type: 1,
      reply_placeholder: "回复@昵称:主评论",
    });
  },
  //触发回复动作
  replyToSomeOne(e) {
    let who = e.target.dataset.item;
    this.setData({
      reply_type: 2,
      reply_one: who,
      reply_placeholder: "回复@昵称:" + who.nick_name,
    });
  },
  replyContentBtn(e) {
    let replyContent = e.detail.value;
    this.setData({
      reply_content: replyContent,
    });
  },
  //回复评价
  submitReply() {
    //goods.comment.reply-comment
    let that = this;
    let reply_id = "";
    if (this.data.reply_type == 1) {
      reply_id = this.data.comment_id;
    } else {
      reply_id = this.data.reply_one.id;
    }
    let urlStr = app.getNetAddresss("goods.comment.reply-comment");
    urlStr += "&id=" + reply_id;
    urlStr += "&content=" + this.data.reply_content;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let myData = res.data;
          let has_many_reply = that.data.has_many_reply;
          has_many_reply.push(myData);
          that.setData({
            reply_content: "",
            has_many_reply: has_many_reply,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  toGoods (e) {
    let goodsId = e.currentTarget.dataset.goodsid;
    wx.redirectTo({
      url: '/packageA/detail_v2/detail_v2?id=' + goodsId,
    });
  },

});
